commonlibsse_ng\re\c/
Calendar.rs

1mod day;
2mod month;
3mod time;
4mod year;
5
6use core::ffi::c_char;
7
8pub use self::day::{GameDay, Week};
9pub use self::month::{MonthInGame, MonthIndex};
10pub use self::time::{GameDateTime, Hour};
11pub use self::year::Year;
12use crate::re::TESGlobal::TESGlobal;
13
14/// Represents the `Calendar` class from C++.
15#[repr(C)]
16#[derive(Debug)]
17pub struct Calendar {
18    pad01: u8,                        // 0x01
19    pad02: u16,                       // 0x02
20    pad04: u32,                       // 0x04
21    game_year: *mut TESGlobal,        // 0x08
22    game_month: *mut TESGlobal,       // 0x10
23    game_day: *mut TESGlobal,         // 0x18
24    game_hour: *mut TESGlobal,        // 0x20
25    game_days_passed: *mut TESGlobal, // 0x28
26    time_scale: *mut TESGlobal,       // 0x30
27    midnights_passed: u32,            // 0x38
28    raw_days_passed: f32,             // 0x3C
29}
30
31const _: () = {
32    assert!(core::mem::size_of::<Calendar>() == 0x40);
33};
34
35impl Calendar {
36    /// Gets the singleton instance of `Calendar`.
37    #[commonlibsse_ng_derive_internal::relocate(
38        cast_as = "*mut *mut Calendar",
39        default = "None",
40        deref_once,
41        id(se = 514287, ae = 400447)
42    )]
43    pub fn get_singleton() -> Option<&'static Calendar> {
44        |deref_type: DerefType| unsafe { deref_type.as_ref() }
45    }
46
47    /// Gets the current game time.
48    #[inline]
49    pub fn get_current_game_time(&self) -> Option<Hour> {
50        debug_assert!(crate::rex::win32::is_accessible_struct(self.game_days_passed));
51        unsafe { self.game_days_passed.as_ref().map(|g| Hour::new(g.value)) }
52    }
53
54    /// Gets the current day.
55    #[inline]
56    pub fn get_day(&self) -> Option<GameDay> {
57        debug_assert!(crate::rex::win32::is_accessible_struct(self.game_day));
58        unsafe { self.game_day.as_ref().map(|g| GameDay::new(g.value)) }
59    }
60
61    /// Gets the day name.
62    #[inline]
63    pub fn get_day_name(&self) -> Option<&'static str> {
64        self.get_day_of_week().map(|day_of_week| day_of_week.as_str())
65    }
66
67    /// Gets the day of the week.
68    #[inline]
69    pub fn get_day_of_week(&self) -> Option<Week> {
70        self.get_days_passed().and_then(|day| day.to_week())
71    }
72
73    /// Gets the number of days passed.
74    #[inline]
75    pub fn get_days_passed(&self) -> Option<GameDay> {
76        unsafe { self.game_days_passed.as_ref().map(|g| GameDay::new(g.value)) }
77    }
78
79    /// Gets the time string.
80    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 35413, ae_id = 36311)]
81    pub fn get_time_date_string(dest: *mut c_char, max: u32, show_year: bool) {}
82
83    /// Gets the current hour.
84    #[inline]
85    pub fn get_hour(&self) -> Option<Hour> {
86        unsafe { self.game_hour.as_ref().map(|g| Hour::new(g.value)) }
87    }
88
89    /// Gets the number of hours passed.
90    #[inline]
91    pub fn get_hours_passed(&self) -> Option<f32> {
92        Some(self.get_days_passed()?.0 * 24.0)
93    }
94
95    /// Gets the number of hours per day.
96    #[commonlibsse_ng_derive_internal::relocate_fn(se_id = 241610, ae_id = 195681)]
97    pub fn get_hours_per_day() -> f32 {}
98
99    /// Gets the current minutes.
100    #[inline]
101    pub fn get_minutes(&self) -> Option<u32> {
102        Some(self.get_hour()?.to_minutes())
103    }
104
105    /// Gets the current month.
106    #[inline]
107    pub fn get_month(&self) -> Option<MonthIndex> {
108        unsafe { self.game_month.as_ref().map(|g| MonthIndex::new(g.value)) }
109    }
110
111    /// Gets the month name.
112    #[inline]
113    pub fn get_month_name(&self) -> Option<&'static str> {
114        self.get_month()?.to_enum().map(|month| month.as_str())
115    }
116
117    /// Gets the ordinal suffix for the day.
118    #[inline]
119    pub fn get_ordinal_suffix(&self) -> Option<&'static str> {
120        Some(self.get_day()?.ordinal_suffix())
121    }
122
123    /// Gets the in-game time as a `NaiveDateTime`.
124    pub fn get_time(&self) -> Option<GameDateTime> {
125        let year = self.get_year()?;
126        let month = self.get_month()?;
127        let day = self.get_day()?;
128        let hour = self.get_hour()?;
129        GameDateTime::new(year, month, day, hour)
130    }
131
132    /// Gets the current time scale.
133    #[inline]
134    pub fn get_timescale(&self) -> f32 {
135        unsafe { self.time_scale.as_ref().map_or(1.0, |g| g.value) }
136    }
137
138    /// Gets the current year.
139    #[inline]
140    pub fn get_year(&self) -> Option<Year> {
141        unsafe { self.game_year.as_ref().map(|g| Year::new(g.value)) }
142    }
143}